package at.fhooe.mc;

import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replay;
import static org.powermock.api.easymock.PowerMock.verify;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import at.fhooe.mc.control.RemoteElevatorAdapter;

@RunWith(PowerMockRunner.class)
@PrepareForTest({RemoteElevatorAdapter.class,Naming.class,JOptionPane.class})
public class PowerMockRemoteElevatorAdapterTest {

	@Test
	public void testInit() throws MalformedURLException, RemoteException,
			NotBoundException {
		mockStatic(Naming.class);

		expect(Naming.lookup("rmi://localhost/ElevatorSim")).andReturn(null);

		replay(Naming.class);

		RemoteElevatorAdapter adapter = new RemoteElevatorAdapter();
		assertTrue(adapter.init());

		verify(Naming.class);
	}

	@Test
	public void testInitException() throws MalformedURLException,
			RemoteException, NotBoundException {
		mockStatic(Naming.class);
		mockStatic(JOptionPane.class);

		expect(Naming.lookup("rmi://localhost/ElevatorSim")).andThrow(
				new RemoteException());
		JOptionPane.showMessageDialog(null,
			"Unable to connect to Remote Elevator Device","Remote Error",JOptionPane.ERROR_MESSAGE);

		PowerMock.replayAll(Naming.class,JOptionPane.class);

		RemoteElevatorAdapter adapter = new RemoteElevatorAdapter();
		assertFalse(adapter.init());

		PowerMock.verifyAll();
	}
}
